//
//  WelcomViewController.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/10.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit
import SDWebImage
class WelcomViewController: UIViewController {
    
    
    //MARK: - 懒加载控件
    /// 背景图
    private lazy var imageView = UIImageView(imageName: "ad_background")
/// 头像iamge
    private lazy var iconView:UIImageView = {
        let iv = UIImageView(imageName: "avatar_default_big")
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        return iv
    }()
    
    ///lable
    private lazy var nameLabel = UILabel(txt: "欢迎归来", txtFont: 15)

    override func loadView() {
        view = imageView
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 200)
        }
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options:[],animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            self.nameLabel.alpha = 0
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.nameLabel.alpha = 1
                    }, completion: { (_) -> Void in
//                    //一秒之后
//                  //发送消息,更换主窗口
            NSNotificationCenter.defaultCenter().postNotificationName(LQChangeRootViewController, object: "main")
                        
                })
        }
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //异步加载头像
        let url = NSURL(string: (UserAccountViewModel.shareAccountModel.account?.avatarLarge)!)
//        print((UserAccountViewModel.shareAccountModel.account?.avatarLarge)!)
        iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        nameLabel.text = UserAccountViewModel.shareAccountModel.account?.screenName
        
    }

}
// MARK: - 更新UI
extension WelcomViewController {
    private func setupUI () {
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.centerY.equalTo(view.snp_bottom).offset(-200)
            make.height.equalTo(90)
            make.width.equalTo(90)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(iconView.snp_bottom).offset(10)
        }
//        nameLabel.hidden = true
        
    }
}